const Comment = require('../db').Comment
const Commit = require('../db').Commit
module.exports = {
    async add(ctx, next) {
        let { compose_id, content, to_user_id} = ctx.request.body;
        if ( !compose_id) {
            ctx.body = {
                code: 500,
                msg: '未指定评论的内容!'
            }
            return 
        }
        try {
            let newComment = new Comment({
                compose_id,
                content,
                to_user:to_user_id,
                from_user:ctx._id,
                like:0
            })
            let res = await newComment.save()
            if (res.id != null) {
                let re = await Comment.findOne({_id: res.id})
                .populate({
                    path:'from_user',
                    select: '_id avatar user_id user_name company',
                    model:'User',
                })
                .populate({
                    path:'to_user',
                    select: '_id avatar user_id user_name company',
                    model:'User',
                })
                ctx.body = {
                    code: 200,
                    msg: '成功!',
                    data: re
                }
                await Commit.findOneAndUpdate({_id: compose_id}, {
                    $inc: { 'comment': 1 }}
                )
            } else {
                ctx.body = {
                    code: 500,
                    msg: '创建失败，服务器异常，请稍后再试!'
                  }
            }
        } catch(e) {
            console.log(e)
            ctx.body = {
                code: 500,
                msg: '服务器异常，请稍后再试!'
            }
        }
    },
    async get(ctx, next) {
        let { id = null } = ctx.request.query;
        if (!id) {
            ctx.body = {
                code: 500,
                msg: '未指定查询的ID!'
            }
            return 
        }
        try {
            let res = await Comment.find({compose_id:id})
            .populate({
                path:'from_user',
                select: '_id avatar user_id user_name company',
                model:'User',
            })
            .populate({
                path:'to_user',
                select: '_id avatar user_id user_name company',
                model:'User',
            })
            .sort({'_id':-1})
            if (res.length !== 0) {
                ctx.body = {
                    code: 200,
                    msg: '成功!',
                    data: res.map(item => {
                        if (!item.to_user) {
                            item.to_user = null
                        }
                        return item 
                    }) 
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '无数据!' 
                }
            }
        }
        catch(e) {
            console.log(e)
            ctx.body = {
                code: 500,
                msg: e
            }
        }
    }
}
